<?php




/**
* categories Class
*/

class categories
extends entity
{
	/**
	* Optional Constructor: Load on demand only.
	*/
	public function __construct()
	{
		# Parent's default constructor is necessary.
		parent::__construct();

		/**
		* Set Private, Protected or Public Members
		*/
		$this->protection_code = 'b766457702d1144c5c7438782dd63290'; # Some random text, valid for the entire life
		$this->table_name = 'gallery_categories'; # Name of this table/entity name
		$this->pk_column = 'category_id'; # Primary Key's Column Name

		/**
		* Validation fields as used in add/edit forms
		*/
		$this->fields = array(
			# Remove the columns that you do not want to use in the ADD form
			'add' => array(
				'dimension_width' => null,
				'dimension_height' => null,
				'category_name' => null,
				'category_keywords' => null,
				'category_coverimage' => null,
				'category_description' => null,
			),
			
			# Remove the columns that you do not want to use in the EDIT form
			'edit' => array(
				'dimension_width' => null,
				'dimension_height' => null,
				'category_name' => null,
				'category_keywords' => null,
				'category_coverimage' => null,
				'category_description' => null,
			),
		);
	}

	/**
	* List entries from [ categories ]
	* Column `code` signifies a protection code while deleting/editing a record
	* @param $conditions SQL Conditions
	* @return Multi-Dimensional array of entries in the list
	*/
	public function list_entries(condition $condition, $from_index=0, $per_page=50)
	{
		$crud = new crud();

		/**
		* Conditions are Compiled here so that we can manupulate them individually.
		* And make them fit for [ categories ] only.
		*/
		$conditions_compiled_AND = $crud->compile_conditions(
			$condition->get_condition('AND'),
			false, 'AND', 1
		);
		$conditions_compiled_OR = $crud->compile_conditions(
			$condition->get_condition('OR'),
			false, 'OR', 2
		);

		$from_index = (int)$from_index;
		$per_page   = (int)$per_page;
		$variable   = new variable(); # It may be necessary to read list out data of a user

		$listing_sql="
SELECT SQL_CALC_FOUND_ROWS
	e.`category_id`, # Do not remove this
	
	# Modify these columns to your own list(e.*)
	e.`dimension_width`,
	e.`dimension_height`,
	e.`category_name`,
	e.`category_keywords`,
	e.`category_coverimage`,
	
	e.`images_counter`,
	e.is_approved,
	
	MD5(CONCAT(`category_id`, '{$this->protection_code}')) `code` # Protection Code
FROM `gallery_categories` `e`
WHERE
	(
		{$conditions_compiled_AND}
	)
	AND (
		{$conditions_compiled_OR}
	)
ORDER BY
	# We assume that the sorting fields are available
	#`sink_weight` ASC,
	category_name,
	`category_id` DESC
LIMIT {$from_index}, {$per_page}
;";
		$this->query($listing_sql);
		$entries = $this->to_array();

		# Pagination helper: Set the number of entries
		$counter_sql="SELECT FOUND_ROWS() total;"; # Uses SQL_CALC_FOUND_ROWS from above query. So, run it immediately.
		$totals = $this->row($counter_sql);
		$this->total_entries_for_pagination = $totals['total'];

		return $entries;
	}
	
	/**
	* Details of an entity in [ categories ] for management activities only.
	* @param $pk integer Primary Key's value of an entity
	* @return $details Associative Array of Detailed records of an entity
	*/
	public function details($category_id=0)
	{
		global $subdomain_id;
		$category_id = (int)$category_id;
		$details_sql="
SELECT
	e.`category_id`, # Do not remove this

	e.*, # Modify these columns,
	
	# Admin must have it to EDIT the records
	MD5(CONCAT(`category_id`, '{$this->protection_code}')) `code` # Protection Code
FROM `gallery_categories` `e`
WHERE
	`category_id` = {$category_id}
	# AND subdomain_id={$subdomain_id}
;";
		$details = $this->row($details_sql);
		return $details;
	}

	/**
	* Details of an entity in [ categories ] for public display.
	* @param $pk integer Primary Key's value of an entity
	* @return $details Associative Array of Detailed records of an entity
	*/
	public function get_details($category_id=0, $protection_code='')
	{
		$protection_code = $this->sanitize($protection_code);
		$category_id = (int)$category_id;
		$details_sql="
SELECT
	`category_id`, # Do not remove this

	e.*, # Modify these columns

	MD5(CONCAT(`category_id`, '{$this->protection_code}')) `code` # Protection Code
FROM `gallery_categories` `e`
WHERE
	`category_id` = {$category_id}
	AND e.is_active='Y'

	# Optionally validate
	AND MD5(CONCAT(`category_id`, '{$this->protection_code}')) = '{$protection_code}'
;";
		$details = $this->row($details_sql);
		return $details;
	}

	/**
	* Flag a field; dummy use; unless you use it.
	* Every method should sanitize the user input.
	* It will co-exist with the live features.
	*/
	public function flag_field($category_id=0, $protection_code='', $field_name='')
	{
		# Selected fields allowed to flag Y/N
		if(!in_array($field_name, array('is_approved', 'is_featured')))
			return false;
		
		$protection_code = $this->sanitize($protection_code);
		$category_id = (int)$category_id;
		$subdomain_id = framework::subdomain_id();

		$flag_sql="
UPDATE `gallery_categories` SET
	# Set your flag name here
	`{$field_name}`=IF(`{$field_name}`='Y', 'N', 'Y')
WHERE
	`category_id` = {$category_id}
	AND subdomain_id={$subdomain_id}
	
	# Don't touch the deleted flags
	AND is_active='Y'

	# Optionally validate
	AND MD5(CONCAT(`category_id`, '{$this->protection_code}')) = '{$protection_code}'
;";
		return $this->query($flag_sql);
	}

	/**
	* Welcome and ask for authentication?
	* Please extend this method according to your business logic.
	* Used right after a new categories is added.
	*/
	public function welcome_first($category_id=0)
	{
		$category_id = (int)$category_id;
	 	
		return true;
	}
}
?>